<!DOCTYPE html>
<html>
<head>
<title>开始考试</title>
<link href="css/css2.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/checklogin.js"></script>
<style type="text/css">
<!--
.STYLE3 {
	font-size: 18px;
}

.STYLE4 {
	font-size: 18px;
	font-weight: bold;
}

.STYLE5 {
	color: #FF0000
}

.bt {
	font-size: 19px;
}
-->
</style>

<script type="text/javascript">
	var ksTime; //定义考试时间以分钟计算
	if (readCookie("ss") == "") {
		setCookie("ss", new Date(), ksTime / 60);
	}
	function sT() {
		var tti = new Date();
		var lt = parseInt((tti - new Date(readCookie("ss"))) / 1000);

		lm = Math.floor(lt / 60);
		ls = lt % 60;
		allY = ksTime * 60 - lt;
		ym = Math.floor(allY / 60);
		ys = allY % 60;
		document.getElementById("tTime").innerHTML = "离考试结束还有：" + ym + "分" + ys
				+ "秒";
		if (ym == 0 && ys == 1) {
			alert("考试结束，即将提交试卷");
			submitf();
		}
		var ttt = setTimeout("sT()", 1000);

	}
	function readCookie(name) {
		var cookieValue = "";
		var search = name + "=";
		if (document.cookie.length > 0) {
			offset = document.cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1)
					end = document.cookie.length;
				cookieValue = document.cookie.substring(offset, end)
			}
		}
		return cookieValue;
	}
	function setCookie(name, value, hours) {
		var expire = "";
		if (hours != null) {
			expire = new Date((new Date()).getTime() + hours * 3600000);
			expire = "; expires=" + expire.toGMTString();
		}
		document.cookie = name + "=" + value + expire;
	}
</script>
<script type="text/javascript">
	var sdelay = 0;
	function returnTop() {
		window.scrollBy(0, -100);//Only for y vertical-axis
		if (document.body.scrollTop > 0) {
			sdelay = setTimeout('returnTop()', 50);
		}
	}
</script>
<script type="text/javascript">
	window.onunload = clear;
	function clear() {
		setCookie("ss", new Date(), 0);
	}
	var questions = new Array();
	var ansid;
	var result = new Object();
	$(document).ready(function() {
		$("#button").attr("disabled", false);
		getQestions();
		$("#button").click(function() {
			submit();
			returnTop();
		});
		$("#return").click(function() {
			if (confirm("确定要放弃考试吗")) {
				window.location.href = 'home.html';
				setCookie("ss", new Date(), 0);
			}
		});
		$("#retry").click(function() {
			if (confirm("确定要重新开始考试吗")) {
				window.location.reload();
				setCookie("ss", new Date(), 0);
			}
		});
	});
	var id = new Array();
	var ans = new Array();
	function submit() {
		if (confirm("确定提交吗？请仔细检查")) {
			var rightnum = 0;
			for (var i = 0; i < questions.length; i++) {
				var name = "qs" + i;
				var answer = $('input[name=' + name + ']:checked').val();
				id[i] = questions[i].questionid;
				ans[i] = answer;
				if (answer == parseFloat(questions[i].tanswer).toFixed(2)) {
					rightnum++;
					$("#questiont" + i + "").append(
							"<span style='color:green;'>回答正确</span>");
				} else {
					$("#questiont" + i + "").append(
							"<span style='color:red;'>正确答案为: "
									+ parseFloat(questions[i].tanswer).toFixed(
											2) + "</span>");
				}
			}

			$("#score").html("成绩：" + rightnum * 100 / questions.length + "分");
			result.score = rightnum * 100 / questions.length;
			result.qid1 = id[0];
			result.qid2 = id[1];
			result.qid3 = id[2];
			result.qid4 = id[3];
			result.qid5 = id[4];
			result.qid6 = id[5];
			result.qid7 = id[6];
			result.qid8 = id[7];
			result.qid9 = id[8];
			result.qid10 = id[9];
			result.qid11 = id[10];
			result.qid12 = id[11];
			result.qid13 = id[12];
			result.qid14 = id[13];
			result.qid15 = id[14];
			result.qid16 = id[15];
			result.qid17 = id[16];
			result.qid18 = id[17];
			result.qid19 = id[18];
			result.qid20 = id[19];

			result.ans1 = ans[0];
			result.ans2 = ans[1];
			result.ans3 = ans[2];
			result.ans4 = ans[3];
			result.ans5 = ans[4];
			result.ans6 = ans[5];
			result.ans7 = ans[6];
			result.ans8 = ans[7];
			result.ans9 = ans[8];
			result.ans10 = ans[9];
			result.ans11 = ans[10];
			result.ans12 = ans[11];
			result.ans13 = ans[12];
			result.ans14 = ans[13];
			result.ans15 = ans[14];
			result.ans16 = ans[15];
			result.ans17 = ans[16];
			result.ans18 = ans[17];
			result.ans19 = ans[18];
			result.ans20 = ans[19];
			console.log(result);
			$("#button").attr("disabled", true);
			setCookie("ss", new Date(), 0);
			document.cookie = $.ajax({
				type : "POST",
				url : "../test/submit1",
				dataType : "json",
				data : JSON.stringify(result),
				contentType : "application/json",
				success : function(data) {
				}
			});

		} else {
			return false;
		}
	}
	function submitf() {

		var rightnum = 0;
		for (var i = 0; i < questions.length; i++) {
			var name = "qs" + i;
			var answer = $('input[name=' + name + ']:checked').val();
			id[i] = questions[i].questionid;
			ans[i] = answer;
			if (answer == parseFloat(questions[i].tanswer).toFixed(2)) {
				rightnum++;
			} else {
				$("#questiont" + i + "").append(
						"<span style='color:red;'>正确答案为: "
								+ parseFloat(questions[i].tanswer).toFixed(2)
								+ "</span>");
			}
		}

		$("#score").html("成绩：" + rightnum * 100 / questions.length + "分");
		$("#button").attr("disabled", true);
		result.score = rightnum * 100 / questions.length;
		result.qid1 = id[0];
		result.qid2 = id[1];
		result.qid3 = id[2];
		result.qid4 = id[3];
		result.qid5 = id[4];
		result.qid6 = id[5];
		result.qid7 = id[6];
		result.qid8 = id[7];
		result.qid9 = id[8];
		result.qid10 = id[9];
		result.qid11 = id[10];
		result.qid12 = id[11];
		result.qid13 = id[12];
		result.qid14 = id[13];
		result.qid15 = id[14];
		result.qid16 = id[15];
		result.qid17 = id[16];
		result.qid18 = id[17];
		result.qid19 = id[18];
		result.qid20 = id[19];

		result.ans1 = ans[0];
		result.ans2 = ans[1];
		result.ans3 = ans[2];
		result.ans4 = ans[3];
		result.ans5 = ans[4];
		result.ans6 = ans[5];
		result.ans7 = ans[6];
		result.ans8 = ans[7];
		result.ans9 = ans[8];
		result.ans10 = ans[9];
		result.ans11 = ans[10];
		result.ans12 = ans[11];
		result.ans13 = ans[12];
		result.ans14 = ans[13];
		result.ans15 = ans[14];
		result.ans16 = ans[15];
		result.ans17 = ans[16];
		result.ans18 = ans[17];
		result.ans19 = ans[18];
		result.ans20 = ans[19];
		console.log(result);
		setCookie("ss", new Date(), 0);
		$.ajax({
			type : "POST",
			url : "../test/submit1",
			dataType : "json",
			data : JSON.stringify(result),
			contentType : "application/json",
			success : function(data) {
			}
		});
	}
	function GetRandomNum(Min, Max) {
		var Range = Max - Min;
		var Rand = Math.random();
		while (Min + Math.round(Rand * Range) == 0) {
			Rand = Math.random();
		}
		return (Min + Math.round(Rand * Range));
	}
	function GetRandomNum2(Min, Max) {
		var Range = Max - Min;
		var Rand = Math.random();
		Rand = Math.random();
		return (Min + Math.round(Rand * Range));
	}
	function getQestions() {
		$
				.ajax({
					type : "GET",
					url : "../test/get",
					dataType : "json",
					contentType : "application/json",
					success : function(data) {

						var list = data.qList;
						result.testid = data.test.testid;
						result.time = "";
						for (var i = 0; i < list.length; i++) {
							questions[i] = list[i];
						}
						ansid = questions[1].ansid;
						var html = "", j;
						var answer = new Array();
						for (var i = 0; i < questions.length; i++) {

							j = i + 1;
							answer[0] = (parseFloat(questions[i].tanswer) + parseInt(GetRandomNum(
									-20, 20))).toFixed(2);
							answer[1] = (parseFloat(questions[i].tanswer) + parseInt(GetRandomNum(
									-20, 20))).toFixed(2);
							answer[2] = (parseFloat(questions[i].tanswer) + parseInt(GetRandomNum(
									-20, 20))).toFixed(2);
							answer[3] = (parseFloat(questions[i].tanswer) + parseInt(GetRandomNum(
									-20, 20))).toFixed(2);
							var q = GetRandomNum2(0, 3);
							answer[q] = parseFloat(questions[i].tanswer)
									.toFixed(2);
							html = html
									+ "<tr><td colspan='3' id='question"+i+"' class='tt'>第"
									+ j
									+ "题、"
									+ questions[i].qdetail
									+ "=</td><td id='questiont"+i+"'></td></tr>";
							html = html
									+ "<tr>"
									+ "<td ><strong>A．</strong><input class='radiotd' name='qs"+i+"' type='radio' value="+answer[0]+">"
									+ answer[0]
									+ "</input></td>"
									+ "<td ><strong>B．</strong><input class='radiotd' name='qs"+i+"' type='radio' value="+answer[1]+">"
									+ answer[1]
									+ "</input></td>"
									+ "<td ><strong>C．</strong><input class='radiotd' name='qs"+i+"' type='radio' value="+answer[2]+">"
									+ answer[2]
									+ "</input></td>"
									+ "<td><strong>D．</strong><input class='radiotd' name='qs"+i+"' type='radio' value="+answer[3]+">"
									+ answer[3] + "</input></td>" + "</tr>";

						}
						$("#tb").append(html);
						$("#button").show();
						$("#return").show();
						$("#retry").show();
						$("#txt").show();
						ksTime = 3;//设置时间 这里设置为0.1代表是6秒,测试用
						sT();
					}

				});
	}
</script>

</head>

<body onload="haslogin()">
	<table width="1003" height="485" border="0" cellpadding="0"
		cellspacing="0" class="centerbg">
		<tr>
			<td width="149" height="485">&nbsp;</td>
			<td width="741" valign="top" class="rightbian">
				<table width="98%" border="0" align="center" cellpadding="0"
					cellspacing="10" id="tb">
					<tr>
						<td><div align="center" class="STYLE3">考试时间：30 分钟</div></td>
						<td><div align="center" class="STYLE3">总分 ：100 分</div></td>
						<td id="score" align="center" class="STYLE3"></td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td colspan="2"><div id="tTime"></div></td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td colspan="3" bgcolor="#999999" class="STYLE4">选择题(每小题5分，共20个)</td>
					</tr>
					</tr>
				</table>
				<div style="text-align: center;">
					<input type="submit" id="button" class="bt" value="提交试卷"
						hidden="hidden" />&nbsp;&nbsp;&nbsp;
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit"
						id="return" class="bt" value="返回主页" hidden="hidden" />
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
						type="submit" id="retry" class="bt" value="重新考试" hidden="hidden" />
				</div>
			</td>
			<td width="113">&nbsp;</td>
		</tr>
	</table>
	<div>&nbsp;</div>
</body>
</html>
